package com.ruicar.afs.cloud.manage.exempt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.basic.api.dto.BatchPenaltyExemptDetailsDTO;
import com.ruicar.afs.cloud.basic.api.fegin.exempt.BatchPenaltyExemptService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.manage.common.entity.ManageBatchPenaltyExemptDetails;
import com.ruicar.afs.cloud.manage.common.service.ManageBatchPenaltyExemptDetailsService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 批量罚息豁免
 */
@Slf4j
@RestController
@RequestMapping("/batchExemptPenalty")
@AllArgsConstructor
public class BatchExemptPenaltyController {
    private final ManageBatchPenaltyExemptDetailsService manageBatchPenaltyExemptDetailsService;
    private final BatchPenaltyExemptService batchPenaltyExemptService;
    @ApiOperation(value = "多条件查询")
    @PostMapping("/findMulticondition")
    public IResponse findByContractNo(@RequestBody BatchPenaltyExemptDetailsDTO dto){

        if(ObjectUtils.isNotEmpty(dto.getBatchNo())){
            //有批次号，则查询历史申请记录
            IPage<BatchPenaltyExemptDetailsDTO> list = manageBatchPenaltyExemptDetailsService.page(new Page(dto.getPageNumber()
                    ,dto.getPageSize()),Wrappers.<ManageBatchPenaltyExemptDetails>lambdaQuery()
                    .eq(ManageBatchPenaltyExemptDetails::getBatchNo, dto.getBatchNo())
                    .eq(ObjectUtils.isNotEmpty(dto.getApplyNo()), ManageBatchPenaltyExemptDetails::getApplyNo, dto.getApplyNo())
                    .eq(ObjectUtils.isNotEmpty(dto.getBusinessType()), ManageBatchPenaltyExemptDetails::getBusinessType, dto.getBusinessType())
                    .eq(ObjectUtils.isNotEmpty(dto.getCity()), ManageBatchPenaltyExemptDetails::getCity, dto.getCity())
                    .eq(ObjectUtils.isNotEmpty(dto.getContractNo()), ManageBatchPenaltyExemptDetails::getContractNo, dto.getContractNo())
                    .ge(ObjectUtils.isNotEmpty(dto.getStartDate()), ManageBatchPenaltyExemptDetails::getDueDate, dto.getStartDate())
                    .le(ObjectUtils.isNotEmpty(dto.getEndDate()), ManageBatchPenaltyExemptDetails::getDueDate, dto.getEndDate())
                    .eq(ObjectUtils.isNotEmpty(dto.getRegion()), ManageBatchPenaltyExemptDetails::getRegion, dto.getRegion())
                    .eq(ObjectUtils.isNotEmpty(dto.getProductId()), ManageBatchPenaltyExemptDetails::getProductId, dto.getProductId()));

            return new IResponse().setData(list);
        }else{
            //无批次号，则是新增
            IResponse<List<BatchPenaltyExemptDetailsDTO>> batchPenaltyExemptDetails = batchPenaltyExemptService.batchPenaltyExempt(dto);
            return new IResponse().setData(batchPenaltyExemptDetails.getData());
        }

    }
}
